package com.webster.isw.core.service.impl;

import com.webster.isw.core.config.SysParamDef;
import com.webster.isw.core.gen.entity.ScSysParam;
import com.webster.isw.core.gen.mapper.ScSysParamMapper;
import com.webster.isw.core.service.ParamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 系统全局参数
 * Created by Webster on 2016-5-6.
 */
@Service("paramService")
public class ParamServiceImpl implements ParamService {
    /**
     * Field tgpm: SERVER端参数定义
     */
    @Resource
    private ScSysParamMapper scSysParamMapper;

    /**
     * Field systemParam: 默认参数定义
     */
    @Resource
    private SysParamDef systemParam;

    /**
     * Description: 返回服务端参数,优先获取数据库中的配置,
     *
     * @param id 参数名称
     * @return 值
     */
    @Override
    public String getSysParamPreFerDb(String id) {
        String re;
        ScSysParam scSysParam = scSysParamMapper.selectByPrimaryKey(id);
        if (null != scSysParam) {
            re = scSysParam.getValue();
        } else {
            re = getSysParamDefault(id);
        }
        return re;
    }

    /**
     * Description: 返回服务端参数,优先获取默认配置,
     *
     * @param id 参数名称
     * @return 值
     */
    @Override
    public String getSysParamPreFerDefault(String id) {
        String re = getSysParamDefault(id);
        if (null == re) {
            ScSysParam scSysParam = scSysParamMapper.selectByPrimaryKey(id);
            if (null != scSysParam) {
                re = scSysParam.getValue();
            }
        }
        return re;
    }

    /**
     * Description: 从数据库中获取系统参数
     *
     * @param code 参数名称
     * @return 参数对象
     */
    @Override
    public String getSysParamFromDb(String id) {
        return this.scSysParamMapper.selectByPrimaryKey(id).getValue();
    }

    /**
     * Description: 获取系统默认参数
     *
     * @param code 参数名称
     * @return 参数对象
     */
    @Override
    public String getSysParamDefault(String id) {
        return systemParam.getParams().get(id);
    }

}
